package com.system.service.miniprogram.upload.impl;

import com.system.constant.HTTPStatus;
import com.system.response.Result;
import com.system.service.miniprogram.upload.UploadService;
import com.system.util.QiniuUtil;
import com.system.util.StringUtils;
import lombok.extern.log4j.Log4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Service
@Log4j
public class UploadServiceImpl implements UploadService {

    @Override
    public Result uploadImageToQiniu(MultipartFile[] files) {
        if (files.length==0 || files == null) return new Result().setCode(HTTPStatus.BAD_REQUEST).setMessage("请选择图片!");

        List<String> listImageUrl = new ArrayList<>();
        for (MultipartFile file : files) {
            try {
                String imageUrl = QiniuUtil.saveImage(file, "miniprogram/issueArticle/");
                listImageUrl.add(imageUrl);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return new Result().setCode(HTTPStatus.SUCCESS).setData(listImageUrl);
    }

    @Override
    public Result deleteImageAndQiniu(String fileName) {
        if (StringUtils.isNull(fileName)) return new Result().setCode(HTTPStatus.BAD_REQUEST).setMessage("请选择图片!");
        QiniuUtil.delImages(fileName);
        return new Result().setCode(HTTPStatus.SUCCESS).setMessage("删除成功！");
    }
}
